#ifndef _LIST_HPP_
#define _LIST_HPP_

#include "_linked_list.hpp"

template <typename T>
class List
{
public:
  List(){
    INIT_LIST_HEAD(&head.link);
    _size=0;
  }  
  void                                              append(const T & data){
    _size++;
    Node<T> *node=new Node<T>;
    node->data=data;
    list_add_tail(&node->link,&head.link);
  }
  void                                              add(const T & data){
    _size++;
    Node<T> *node=new Node<T>;
    node->data=data;
    list_add(&node->link,&head.link);
  }
  Node<T> *                                         begin(){
    return list_entry(head.link.next,Node<T>,link);
  }  
  Node<T> *                                         end(){
    return &head;
  }
  void                                              remove(Node<T>*node){
    list_del(&node->link);
    _size--;
  }
  void                                              removeCurrentNode(Node<T>**posptr){
    (*posptr)=list_entry((*posptr)->link.prev,Node<T>,link);
    remove(list_entry((*posptr)->link.next,Node<T>,link));        
  }
  int                                               size(){
    return _size;
  }  
private:
  Node<T>                                           head;
  int                                               _size;
};  


#define __START(listptr,type) Node<type>*pos=(listptr)->begin()

#define __JUDGE(listptr) (pos!=listptr->end())

#define __NEXT(type)  pos=list_entry(pos->link.next,Node<type>,link)


#define ITERATE(listptr,type)   for(__START(listptr,type);__JUDGE(listptr);__NEXT(type))        

#define REMOVE(listptr) listptr->removeCurrentNode(&pos)

#define VALUE pos->data                          




#endif
